import com.sun.deploy.util.StringUtils;

/**
 * @author 03010570
 * @date 2020/07/06
 * describe:    LeetCode ： 63、 不同路径2 https://leetcode-cn.com/problems/unique-paths-ii/
 */
public class LeetCode_63 {

    public static void main(String[] args) {
        String businessKey = "SSD20200706012";
        System.out.println(businessKey.startsWith("SSD"));
        System.out.println(businessKey.startsWith("SYD"));
        int[][] ints = {
                {0,0},
                {1,1},
                {0,0}
        };
//        int[][] ints = {
//                {0, 0, 0},
//                {0, 1, 0},
//                {0, 0, 0}
//        };
        System.out.println(uniquePathsWithObstacles(ints));
    }

    /**
     * DP 常规解法
     * if ()
     * DP ：  dp[i][j] = dp[i-1][j] + dp[i][j-1]
     *
     * @param obstacleGrid
     * @return
     */
    public static int uniquePathsWithObstacles(int[][] obstacleGrid) {
        if(obstacleGrid[0][0] == 1){
            return 0;
        }
        obstacleGrid[0][0] = 1;
        int rows = obstacleGrid.length;
        int columns = obstacleGrid[0].length;
        for (int i = 1; i < rows; i++) {
            obstacleGrid[i][0] = obstacleGrid[i][0] == 1 ? 0 : obstacleGrid[i-1][0];
        }
        for (int j = 1; j < columns; j++) {
            obstacleGrid[0][j] = obstacleGrid[0][j] == 1 ? 0 : obstacleGrid[0][j-1];
        }
        for (int i = 1; i < rows; i++) {
            for (int j = 1; j < columns; j++) {
                obstacleGrid[i][j] = obstacleGrid[i][j] == 1 ? 0 : (obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1]);
            }
        }
        return obstacleGrid[rows - 1][columns - 1];
    }
}
